﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ControlExplorer
{
    public partial class SampleListPage 
        : PhoneApplicationPage
    {
        public SampleListPage()
        {
            InitializeComponent();
            SelectionTransition.Completed += SelectionTransition_Completed;
        }

        int _controlIndex;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            
            if (int.TryParse(NavigationContext.QueryString["control"], out _controlIndex))
            {
                DataContext = ViewModel.Instance.Controls[_controlIndex];            
            }

            LoadTransition.Begin();
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            DataContext = null;
        }

        private void OnItemSelected(object sender, SelectionChangedEventArgs e)
        {
            if (list.SelectedIndex != -1)
            {
                SelectionTransition.Begin();
                list.IsEnabled = false;
            }
        }

        void SelectionTransition_Completed(object sender, EventArgs e)
        {
            list.IsEnabled = true;
            this.NavigationService.Navigate(new Uri(string.Format("/View/Pages/SamplePage" + App.XamlPageSuffix + ".xaml?control={0}&sample={1}", _controlIndex, list.SelectedIndex), UriKind.Relative));
        }
    }
}